package cn.christ.com.mobilesafe.view.activity_sjfd;

import android.Manifest;
import android.content.Context;
import android.support.annotation.NonNull;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.christ.com.mobilesafe.R;
import cn.christ.com.mobilesafe.common.Global;
import cn.christ.com.mobilesafe.common.utils.LogUtils;
import cn.christ.com.mobilesafe.common.utils.SPUtils;
import cn.christ.com.mobilesafe.constant.Constants;

/**
 * Created by Administrator on 2018/7/21.
 */

public class SetingUpActivity2 extends BaseSetingUpActivity {
    @BindView(R.id.btn_bind_sim)
    Button btnBindSim;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.btn_pre)
    Button btnPre;
    private TelephonyManager tm;
    private String sim;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_setup2;
    }

    @Override
    public void initView() {
        tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        sim = (String) SPUtils.get(Constants.SJFD_BIND_SIM, "");
        if (TextUtils.isEmpty(sim)) {
            btnBindSim.setSelected(false);
        } else {
            btnBindSim.setSelected(true);
        }
    }


    @OnClick({R.id.btn_bind_sim, R.id.btn_next, R.id.btn_pre})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_bind_sim:
                AndPermission.with(getApplicationContext())
                        .requestCode(Constants.READ_PHONE_STATE)
                        .permission(Manifest.permission.READ_PHONE_STATE)
                        .callback(new PermissionListener() {
                            @Override
                            public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                                if (Constants.READ_PHONE_STATE == requestCode) {
                                    bindSim();
                                }
                            }

                            @Override
                            public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                                if (AndPermission.hasAlwaysDeniedPermission(SetingUpActivity2.this, deniedPermissions)) {
                                    AndPermission.defaultSettingDialog(SetingUpActivity2.this, Constants.READ_PHONE_STATE).show();
                                }
                            }
                        })
                        .start();
                break;
            case R.id.btn_next:
                if (TextUtils.isEmpty(sim)) {
                    Global.showToast(getString(R.string.bind_not_sim));
                    return;
                }
                performNext();
                finish();
                overridePendingTransition(R.anim.next_enter, R.anim.next_exit);
                break;
            case R.id.btn_pre:
                performPre();
                finish();
                overridePendingTransition(R.anim.pre_enter, R.anim.pre_exit);
                break;
        }
    }


    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    protected void performPre() {
        startActivityNotFinish(SetingUpActivity1.class);
    }

    @Override
    protected void performNext() {
        startActivityNotFinish(SetingUpActivity3.class);
    }

    /**
     * 绑定sim卡逻辑
     */
    private void bindSim() {
        String sim = (String) SPUtils.get(Constants.SJFD_BIND_SIM, "");
        if (TextUtils.isEmpty(sim)) {//绑定sim卡逻辑
            btnBindSim.setSelected(true);
            String simSerialNumber = tm.getSimSerialNumber();
            LogUtils.d(simSerialNumber);
            SPUtils.put(Constants.SJFD_BIND_SIM, simSerialNumber);
        } else {//不绑定
            btnBindSim.setSelected(false);
            SPUtils.put(Constants.SJFD_BIND_SIM, "");
        }
    }


}
